package com.group.yztcedu.playlive.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.group.yztcedu.playlive.bean.MyDataBean;

import java.util.ArrayList;
import java.util.List;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
	private final static String DB_NAME = "user.db";
	private final static int VERSION = 1;
	public SQLiteDatabase dbConn;

	public MySQLiteOpenHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
		dbConn = this.getReadableDatabase();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table if not exists user(_id integer primary key autoincrement ,headuri,username,userfollownub)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (newVersion > oldVersion) {
			db.execSQL("drop table if exists user");
			onCreate(db);
		}
	}

	/**
	 * @作用：执行带占位符的select语句，查询数据，返回Cursor
	 * @param sql
	 * @param selectionArgs
	 * @return Cursor
	 */
	public Cursor selectCursor(String sql, String[] selectionArgs) {
		return dbConn.rawQuery(sql, selectionArgs);
	}

	/**
	 * @作用：执行带占位符的select语句，返回结果集的个数
	 * @param sql
	 * @param selectionArgs
	 * @return int
	 */
	public int selectCount(String sql, String[] selectionArgs) {
		Cursor cursor = dbConn.rawQuery(sql, selectionArgs);
		if (cursor != null) {
			cursor.moveToFirst();
			int count = cursor.getInt(0);
			cursor.close();
			return count;
		} else {
			return 0;
		}
	}

	/**
	 * @作用：执行带占位符的select语句，返回多条数据，放进List集合中。
	 * @param sql
	 * @param selectionArgs
	 * @return List<Map<String, Object>>
	 */
	public List<MyDataBean> selectList(String sql,
									   String[] selectionArgs) {
		Cursor cursor = dbConn.rawQuery(sql, selectionArgs);
		return cursorToList(cursor);
	}

	/**
	 * @作用：将Cursor对象转成List集合
//	 * @param Cursor
	 *            cursor
	 * @return List<Map<String, Object>>集合
	 */
	public void For(int i,MyDataBean myDataBean,String string){
		switch (i){
			case 1:
				myDataBean.setHeaduri(string);
				break;
			case 2:
				myDataBean.setUsername(string);
				break;
			case 3:
				myDataBean.setUserfollowcount(string);
				break;
		}
	}
	public List<MyDataBean> cursorToList(Cursor cursor) {
		List<MyDataBean> list = new ArrayList<>();
		MyDataBean dataBean = null;
		String[] arrColumnName = cursor.getColumnNames();
		int count = 0;
		while(cursor.moveToNext()){
			dataBean = new MyDataBean();
			for(int i=0;i<arrColumnName.length;i++){
				if(i==0){
					continue;
				}else{
					String string = cursor.getString(i);
					For(i,dataBean,string);
				}
			}
//			ShopschildBean shopschildbean = new ShopschildBean();
//			oldgrupname = newgrupname;
//			if(count>=1){
//				if(oldgrupname.equals(newgrupname)){
//					for(int i=0;i<arrColumnName.length;i++){
//
//						if(i==0){
//							continue;
//						}else if(i==1){
//							newgrupname = cursor.getString(i);
//							dataBean.setShopsname(newgrupname);
//							count++;
//						}
//						String string = cursor.getString(i);
//						For(i,shopschildbean,string);
//					}
//					childelist.add(shopschildbean);
//					dataBean.setList(childelist);
//				}else if(oldgrupname.equals(newgrupname)==false){
//					dataBean = new DataBean();
//					for(int i=0;i<arrColumnName.length;i++){
//						if(i==0){
//							continue;
//						}else if(i==1){
//							newgrupname = cursor.getString(i);
//							dataBean.setShopsname(newgrupname);
//							count++;
//						}
//						String string = cursor.getString(i);
//						For(i,shopschildbean,string);
//					}
//					childelist.add(shopschildbean);
//					dataBean.setList(childelist);
//					list.add(dataBean);
//				}
//			}else if(count==0){
//				dataBean = new DataBean();
//				for(int i=0;i<arrColumnName.length;i++){
//					if(i==0){
//						continue;
//					}else if(i==1){
//						newgrupname = cursor.getString(i);
//						dataBean.setShopsname(newgrupname);
//						count++;
//					}
//					String string = cursor.getString(i);
//					For(i,shopschildbean,string);
//				}
//				childelist.add(shopschildbean);
//				dataBean.setList(childelist);
//				list.add(dataBean);
//			}
			list.add(dataBean);
		}
		if (cursor != null) {
			cursor.close();
		}
		return list;
	}

	/**
	 * @作用：执行带占位符的update、insert、delete语句，更新数据库，返回true或false
	 * @param sql
	 * @param bindArgs
	 * @return boolean
	 */
	public boolean execData(String sql, Object[] bindArgs) {
		try {
			dbConn.execSQL(sql, bindArgs);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * @作用：执行带占位符的update、insert、delete语句，更新数据库，返回true或false
	 * @param
//	 * @param bindArgs
	 * @return boolean
	 */
	public long insert(String headuri,String username,String userfollownub) {
		try {
			//		private String headuri;
//		private String username;
//		private String usersex;
//		private String userdetil;
//		private String usercoin;
//		private String usergift;
			ContentValues values=new ContentValues();
			values.put("headuri",headuri);
			values.put("username",username);
			values.put("userfollownub",userfollownub);
			long user = dbConn.insert("user", null, values);
			return user;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	public void destroy() {
		if (dbConn != null) {
			dbConn.close();
		}
	}
}
